#!/bin/bash

#  Marmoset: a student project snapshot, submission, testing and code review
#  system developed by the Univ. of Maryland, College Park
#
#  Developed as part of Jaime Spacco's Ph.D. thesis work, continuing effort led
#  by William Pugh. See http://marmoset.cs.umd.edu/
#
#  Copyright 2005 - 2011, Univ. of Maryland
#
#  Licensed under the Apache License, Version 2.0 (the "License"); you may not
#  use this file except in compliance with the License. You may obtain a copy of
#  the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations under
#  the License.

function usage() {
    echo "Usage: $0 
	[ -h ] 
	[ -p <port> ] 
	[ -d <database> ]
	[ -l ]
	<database data directory> "
    exit 1
}

case $1 in
    -h | -help | --help )
	usage;;
esac

database=submitserver
PORT=3306
while [ $# -gt 1 ]
do
  case $1 in
      -h | -help | --help )
	  usage
	  ;;
      -p )
	  shift
	  PORT=$1
	  shift;;
      -d )
	  shift
	  database=$1
	  shift;;
      -l )
	  shift
	  echo
	  echo "Careful with this option: You are logging everything!"
	  echo "This will kill your performance and should only be used"
	  echo "for debugging purposes"
	  echo
	  LOG=--log=sql.log
	  ;;
  esac
done

echo "DONE"

if [ "$1" != "" ]; then
    DIR=$1
else
    DIR=$HOME/submitServerDatabase
fi

DATE=`date "+%F-%H-%M-%S"`

echo "STARTING DATABASE $database FROM DATA DIR $DIR ON PORT $PORT"

cd $DIR
cp slow_queries.log slow_queries.log.$DATE
cp submitserver.errlog submitserver.errlog.$DATE
echo "" > slow_queries.log 
echo "" > submitserver.errlog 

$MYSQL_HOME/bin/mysqld_safe \
--no-defaults \
--innodb_data_home_dir=$DIR/$database \
--innodb_data_file_path=$database:10M:autoextend \
--innodb_log_group_home_dir=$DIR/innodb.logs \
--innodb_buffer_pool_size=700M \
--innodb_log_file_size=150M \
--innodb_log_buffer_size=8MB \
--innodb_additional_mem_pool_size=50M \
--innodb_log_files_in_group=3 \
--innodb_file_io_threads=4 \
--binlog-format=MIXED \
--max_allowed_packet=16M \
--port=$PORT \
--socket=$DIR/submitserver.sock \
--log-error=$DIR/submitserver.errlog \
--pid-file=$DIR/submitserver.pid \
--datadir=$DIR \
--slow_query_log_file=$DIR/slow_queries.log \
--max_connections=500 \
--thread_concurrency=50 \
--long_query_time=3 $LOG \
--log-bin=binary.log \
&
